const App = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLogin: false,
        userInfo: {},
        orderCount: {},
        commissionLogs: {},
        hasMore: true,
        page: 1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let _this = this;
        _this.setData({
            isLogin: App.checkIsLogin()
        });
        if (_this.data.isLogin) {
            // 获取当前用户信息
            _this.getUserDetail();
            _this.getCommissionLogs(function (result) {
                _this.setData(result.data);
            });
        }
    },
    getCommissionLogs(callback) {
        let page = this.data.page;
        App._get('agent.commission/index/userId/' + wx.getStorageSync("user_id"), {page: page}, result => {
            callback(result);
        });
    },

    refresh(e) {
        let _this = this;

        console.log('刷新', e)
        this.setData({
            'page': 1,
            'pull.isLoading': true,
            'pull.pullText': '正在加载...'
        });
        this.getCommissionLogs(function (result) {
            _this.setData(result.data);
            setTimeout(() => {
                _this.setData({
                    'pull.isLoading': false,
                    'pull.pullText': '刷新完成'
                })
            }, 300);
        });
    },
    toload(e) {
        if (this.data.hasMore) {
            this.setData({
                'push.isLoading': true,
                'push.pullText': '正在加载...',
            })
            if (this.data.commissionLogs.rows.length < 30) {
                this.setData({
                    'hasMore': false,
                    'push.isLoading': false,
                    'push.pullText': '- 没有更多数据了 -',
                });
            } else {
                page++;
                this.getCommissionLogs(function (result) {
                    let rows = this.data.commissionLogs.rows.concat(result.rows);
                    this.setData({
                        'commissionLogs.rows': rows,
                        'commissionLogs.length': result.length,
                        'push.isLoading': false,
                        'push.pullText': '- 上拉加载更多 -',
                        'push.loading': '../../image/common/finish.png',
                    })
                });
            }
        }

    },
    /**
     * 获取当前用户信息
     */
    getUserDetail() {
        let _this = this;
        App._get('agent.index/detail', {}, result => {
            _this.setData(result.data);
        });
    },

    /**
     * 订单导航跳转
     */
    onTargetOrder(e) {
        let _this = this;
        if (!_this.onCheckLogin()) {
            return false;
        }
        let urls = {
            all: '/pages/agent/order/index?type=all',
            payment: '/pages/agent/order/index?type=payment',
            delivery: '/pages/agent/order/index?type=delivery',
            received: '/pages/agent/order/index?type=received'
        };
        // 转跳指定的页面
        wx.navigateTo({
            url: urls[e.currentTarget.dataset.type]
        })
    },

    /**
     * 菜单列表导航跳转
     */
    onTargetMenus(e) {
        let _this = this;
        if (!_this.onCheckLogin()) {
            return false;
        }
        wx.navigateTo({
            url: '/' + e.currentTarget.dataset.url
        })
    },

    /**
     * 跳转到登录页
     */
    onLogin() {
        wx.navigateTo({
            url: '../login/login',
        });
    },

    /**
     * 验证是否已登录
     */
    onCheckLogin() {
        let _this = this;
        if (!_this.data.isLogin) {
            App.showError('很抱歉，您还没有登录');
            return false;
        }
        return true;
    },


})